#include<stdio.h>
#include<sys/socket.h>
#include<string.h>
#include<unistd.h>

int main(){
	int sv[2];//wen jian miao su fu
	if(socketpair(AF_UNIX,SOCK_STREAM,0,sv)<0){
		perror("socketpair:");
		return -1;
	}

	pid_t id=fork();
	if(id==0){//subprocess
		char *msg="I am a children process!\n";
		char  buffer[1024]={0,};
		close(sv[1]);//close socket 4 , keep socket 3
		
		while(1){
			write(sv[0],msg,strlen(msg));
			sleep(1);
			ssize_t len=read(sv[0],buffer,sizeof(buffer));
			if(len>0){
				buffer[len]='\0';
				printf("children,receive from parent:%s\n",buffer);
			}
		}
	}else if(id>0){//parent process
		char* msg="I am a father process!\n";
                char  buffer[1024]={0,};
                close(sv[0]);//close socket 3 , keep socket 4

                while(1){
                        ssize_t len=read(sv[1],buffer,sizeof(buffer));
                        if(len>0){
                                buffer[len]='\0';
                                printf("father,receive from children:%s\n",buffer);
				sleep(1);
                        }
			write(sv[0],msg,strlen(msg));
		 }
	}else{
		perror("failed to create process!\n");
	}
	return 0;
}
